<template>
    <div>
       
        <table class="table">
            <tbody class="table">
                <tr>
                    <td>编号</td>
                    <td>
                        <input type="text" v-model="data.rnum" disabled>
                    </td>
                </tr>
                <tr>
                    <td><a style="color: red;">*</a>名称</td>
                    <td>
                        <input type="text" v-model="data.rname">
                    </td>
                </tr>
                <tr>
                    <td><a style="color: red;">*</a>服务时长</td>
                    <td>
                        <input type="text" v-model="data.service">
                    </td>
                </tr>
                <tr>
                    <td><a style="color: red;">*</a>服务人数</td>
                    <td>
                        <input type="text" v-model="data.peopleNum">
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" name="s1" :value="true" v-model="data.isStatus">启用
                        <input type="radio" name="s1" :value="false" v-model="data.isStatus">禁用
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="添加" @click="add">
                        <button type="button" class="btn btn-outline-info" @click="back">返回</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import moment from 'moment';
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';


onMounted(()=>{
    num();
})

const  num=()=>{
    var a=moment().format("YYYYMMDD").toString();
    var b=Math.random().toString().substring(2,5)
    data.value.rnum=a+b;
}



const data=ref(
    {
  "rid": 0,
  "rnum": "",
  "rname": "",
  "service": 0,
  "peopleNum": 0,
  "isStatus": true,
  "isDel": false
}
)



const router=useRouter();
const add=()=>{
    if(data.value.rname==""){
        alert("名称不能为空");
        return;
    }
    if(data.value.service==0){
        alert("服务时长不能为空");
        return;
    }

    var s= /^[1-9]\d*$/ 
    if(!s.test(data.value.service.toString())){
        alert("服务时长必须是整数");
        return;
    }
    if(data.value.peopleNum==0){
        alert("服务人数不能为空");
        return;
    }
    if(!s.test(data.value.peopleNum.toString())){
        alert("服务人数必须是整数");
        return;
    }

    

    axios.post("https://localhost:7220/api/Recover/AddRecovery",data.value).then(res=>{
        if(res.data>0){
            alert('添加成功');
            router.push({
                path:'/'
            })
        }else{
            alert('添加失败');
        }
    })
}
const back=()=>{
    router.push({
        path:'/'
    })
}

</script>

<style scoped>

</style>